﻿using System.Web.Mvc;
using System;

namespace VOD.Helpers
{
    public static class HtmlHelpers
    {
        /// <summary>
        /// 截断字符串
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="input">输入的字符串</param>
        /// <param name="length">输入的长度</param>
        /// <returns>返回的字符串，如果被截取，则在后面加...</returns>
        public static string Truncate(this HtmlHelper helper, string input, int length)
        {
            if (String.IsNullOrEmpty(input))
            {
                return input;
            }
            if (input.Length > length)
            {
                input = input.Substring(0, length) + "...";
            }
            return input;
        }
    }

    /// <summary>
    /// HTML编码/解码
    /// </summary>
    public static class HtmlCode
    {
        /// <summary>
        /// HTML编码，如" "转成"&nbsp;" "\r\n"转成"<br />"
        /// </summary>
        /// <param name="content">需转换的内容</param>
        /// <returns>转换后的内容</returns>
        public static string EnCode(string content)
        {
            if (String.IsNullOrEmpty(content))
            {
                return content;
            }
            string str1 = content.Replace("<", "&lt;");
            string str2 = str1.Replace(">", "&gt;");
            string str3 = str2.Replace("  ", "　");
            string str4 = str3.Replace(" ", "&nbsp;");
            string str5 = str4.Replace("\r\n", "<br />");
            return str5; 
        }
        public static string DeCode(string content)
        {
            if (String.IsNullOrEmpty(content))
            {
                return content;
            }
            string str1 = content.Replace("&lt;", "<");
            string str2 = str1.Replace("&gt;", ">");
            string str3 = str2.Replace("&nbsp;&nbsp;", "　");
            string str4 = str3.Replace("&nbsp;", " ");
            string str5 = str4.Replace("<br />", "\r\n");
            return str5;
        } 
    }
}